#include "MenuSystem.h"
#include "IrControl.h"
#include "Menu/Tool/frame.h"

#define ITEM_MUSIC_PLAY		(0)
#define ITEM_MUSIC_BACKWARD	(1)
#define ITEM_MUSIC_FORWARD	(2)
#define ITEM_MUSIC_RND			(3)
#define ITEM_MUSIC_EXIT			(4)

TRect		MediaMusicTouchRect[IMG_MEDIA_MUSIC_ITEM_QTY];
TPosCoor	 MediaMusicItemPos[IMG_MEDIA_MUSIC_ITEM_QTY] =  { {8,376}, {146,376}, {292,376}, {434,376},{576,376} };
TPosCoor MediaMusicDispPos[] = {{8,26},{8,376},{595,30},{547,260}};//bkg,button_bkg,repeat_bkg,time_bkg
int		MediaMusicFocus = -1;
TFrame		MediaMusicFrame;
short		MediaMusicPlayFlag = 0;
char		MediaMusicInfoStr[40];
extern	unsigned char read_info_data[10];
static int 		exit_flag = 0;//0:exit is valid,1:exit is invalid.
extern unsigned char navi_run;
extern  TRect	  MuteInfoPos;
extern int volume_count ;
extern int volume_temp;
extern unsigned char mute_status;
extern unsigned char	VOLvalue ;
extern struct _VolumePos VolumePos;
extern TRect	BarInfoRect;
extern unsigned char rece_handle;	
extern unsigned char dtpresent_flag;
extern pthread_cond_t dt_cond;
extern 		int 	Last_ID ;


int MusicInfoUpdata(void)
{
//	printf ("in movie InfoUpdata !\n");
}
void MediaMusicDrawInfo( TCanvas *pCanvas, int idx)
{
	TRect rect; 

	g_FillRect( pCanvas, 260, 200, 460, 250, RGB_WHITE );
	g_SetTextFormat( FONT16, TEXT_NORMAL, RGB_BLACK, 0, 0 );
	
	switch(idx)
	{
		case ITEM_MUSIC_PLAY:
			if(MediaMusicPlayFlag)
				sprintf(MediaMusicInfoStr, "PLAY");
			else
				sprintf(MediaMusicInfoStr, "PAUSE");			
			break;
		case ITEM_MUSIC_BACKWARD:
			sprintf(MediaMusicInfoStr, "FORWARD");
			break;
		case ITEM_MUSIC_FORWARD:
			sprintf(MediaMusicInfoStr, "BACKWARD");
			break;	
		case ITEM_MUSIC_RND:
			sprintf(MediaMusicInfoStr, "RANDOM");
			break;
	}
	rect.left = 260;
	rect.top = 200;
	rect.right = 460;	
	rect.bottom = 250;	
	g_Puts( pCanvas, FONT33, rect, TEXT_ALIGN_CENTER, MediaMusicInfoStr );
}

void MediaMusicDrawBackgroup(TCanvas *pCanvas)
{	
	char FileName[20];

	DrawJpeg(pCanvas, 0, 0, "/res_jpeg/bkg.jpg" );
	////////////g_FillRect( pCanvas, 0, 0, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1, RGB_BLACK );	
	sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_MUSIC_FOLDER, IMG_MEDIA_MUSIC_UPBKG_COVER);
	DrawJpeg(pCanvas, MediaMusicDispPos[0].x, MediaMusicDispPos[0].y, FileName);
	sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_MUSIC_FOLDER, IMG_MEDIA_MUSIC_BUTBKG_COVER);
	DrawJpeg(pCanvas, MediaMusicDispPos[1].x, MediaMusicDispPos[1].y, FileName);
	
}


void MenuMusicDrawVolume( TCanvas *pCanvas,int flag)
{
	int GetJpegIdx;
	TRect rect;
	TJpeg pJpeg;
	char FileName[20];
	int x,y;
	int i;
	switch (flag)
		{							
			case 1://disp volume all
				GetJpegIdx = IMG_VOLUME_BARCOVER;
				sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);	
				DrawJpeg(pCanvas, VolumePos.Volume_all.x, VolumePos.Volume_all.y, FileName);	
				if (mute_status == 1)
					{
						GetJpegIdx = IMG_VOLUME_MUTE ;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						DrawJpeg(pCanvas, VolumePos.Volume_speaker.x, VolumePos.Volume_speaker.y, FileName);	
					}				
				else
						mute_status = 2;
				
				for (i = 0; i<VOLvalue; i++)
					{
					if (i%2)
						{
						x = VolumePos.Volume_bar_start.x+VolumePos.Distance*(i/2) + VolumePos.Barwidth;
						GetJpegIdx = IMG_VOLUME_RIGHTFOCUS;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						}
					else
						{
						x = VolumePos.Volume_bar_start.x+VolumePos.Distance*(i/2) ;
						GetJpegIdx = IMG_VOLUME_LEFTFOCUS;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						}
					y = VolumePos.Volume_bar_start.y;					
					DrawJpeg(pCanvas,x, y, FileName);						
					}
			break;			
			
			case 2://draw bar
			
			break;
			
			case 3://undisp
				MediaMusicInit();
			break;
		}
	
	pJpeg = GetJpegPtr("/res_jpeg/bar.jpg" );
	rect.left = BARPOS_X;
	rect.right = rect.left + pJpeg.Width;
	rect.top = BARPOS_Y;
	rect.bottom = rect.top + pJpeg.Height;	
	dt_Present_part(rect);	
	volume_count = 0;
	volume_temp = 0;

}


static void VolumeCountHandle(void)
{
	volume_count ++;
		if (volume_count >0xFF00)
			{
				volume_temp ++;
				volume_count = 0;
			}
		if (volume_temp > 10)
			{
				MenuMusicDrawVolume(MediaMusicFrame.pCanvas,3);					
				volume_count = 0;	
				volume_temp = 0;	
				mute_status =0;
			}
}

void MediaMusicDrawItem( TCanvas *pCanvas, int idx, BOOL bfocus )
{
	TJpeg pJpeg;
	TPosCoor pos;
	char FileName[20];
	int GetJpegIdx;
	if( idx < 0 || idx >= IMG_MEDIA_MUSIC_ITEM_QTY )
		return;

	if (idx == ITEM_MUSIC_EXIT)
		{			
			if ( !navi_run)//navi_run :0 close  1: active
				exit_flag = 1;
			else
				exit_flag = 0;
			g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_BLACK, 0, 0 );
			if (exit_flag)//invalid
				{
					GetJpegIdx = IMG_MEDIA_MUSIC_EXIT_VALID ;											
				}
			else
				{
					if( bfocus)
						GetJpegIdx = IMG_MEDIA_MUSIC_ITEM_FOCUS_EXIT ;
					else
						GetJpegIdx = IMG_MEDIA_MUSIC_ITEM_EXIT ;
				}		

				pos = MediaMusicItemPos[idx];
				sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_MUSIC_FOLDER, GetJpegIdx);
				pJpeg = GetJpegPtr(FileName);
				DrawJpeg(pCanvas, pos.x, pos.y, FileName);
				MediaMusicTouchRect[idx].left = pos.x;
				MediaMusicTouchRect[idx].right = pos.x + pJpeg.Width;
				MediaMusicTouchRect[idx].top = pos.y;
				MediaMusicTouchRect[idx].bottom = pos.y + pJpeg.Height;
				/*if (exit_flag)//invalid
					{			
						g_Puts( pCanvas, FONT33, MediaMusicTouchRect[idx], TEXT_ALIGN_CENTER, "EXIT");
					}
					*/
		}
	else
		{
		
			if( bfocus )
				GetJpegIdx = IMG_MEDIA_MUSIC_ITEM_FOCUS_PLAY+idx ;
			else
				GetJpegIdx = IMG_MEDIA_MUSIC_ITEM_PLAY+idx ;
			pos = MediaMusicItemPos[idx];	
			sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_MUSIC_FOLDER, GetJpegIdx);
			pJpeg = GetJpegPtr(FileName);
			DrawJpeg(pCanvas, pos.x, pos.y, FileName);
			MediaMusicTouchRect[idx].left = pos.x;
			MediaMusicTouchRect[idx].right = pos.x + pJpeg.Width;
			MediaMusicTouchRect[idx].top = pos.y;
			MediaMusicTouchRect[idx].bottom = pos.y + pJpeg.Height;
		}
	
}

void MediaMusicDraw( BOOL bDrawAll )
{
	int i;
	if( bDrawAll )
		MediaMusicDrawBackgroup(MediaMusicFrame.pCanvas);
	for( i = 0; i < IMG_MEDIA_MUSIC_ITEM_QTY; i++ )
		MediaMusicDrawItem( MediaMusicFrame.pCanvas, i, FALSE );
	if (mute_status == 1)
		{
			MenuMusicDrawVolume(MediaMusicFrame.pCanvas,1);	
		}
	dtpresent_flag++;		
	pthread_cond_signal(&dt_cond);
	//dt_Present();
}

int MediaMusicGetTouchID( TPosCoor pos )
{
	int i;
	for( i = 0; i < IMG_MEDIA_MUSIC_ITEM_QTY; i++ )
		if( ut_PtInRect( pos, MediaMusicTouchRect[i] ) )
			return i;
	return -1;
}

BOOL MediaMusicBtnEnter(int idx)
{
	unsigned char MediaExit_data [5] = {0xe0,0x03,0x05,0x02,0xea};
	switch(idx)
	{
		case ITEM_MUSIC_PLAY:
			MediaMusicPlayFlag = !MediaMusicPlayFlag;
			MediaMusicDrawInfo( MediaMusicFrame.pCanvas, idx );
			MediaMusicDrawItem( MediaMusicFrame.pCanvas, idx, FALSE );
			//dtpresent_flag++;		
			//pthread_cond_signal(&dt_cond);
			//dt_Present();
			dt_Present_part(MediaMusicTouchRect [idx]);
			break;
		case ITEM_MUSIC_BACKWARD:
			MediaMusicDrawInfo( MediaMusicFrame.pCanvas, idx );
			MediaMusicDrawItem( MediaMusicFrame.pCanvas, idx, FALSE );
			//dtpresent_flag++;		
			//pthread_cond_signal(&dt_cond);
			//dt_Present();
			dt_Present_part(MediaMusicTouchRect [idx]);
			break;
		case ITEM_MUSIC_FORWARD:
			MediaMusicDrawInfo( MediaMusicFrame.pCanvas, idx );
			MediaMusicDrawItem( MediaMusicFrame.pCanvas, idx, FALSE );
			//dtpresent_flag++;		
			//pthread_cond_signal(&dt_cond);
			//dt_Present();
			dt_Present_part(MediaMusicTouchRect [idx]);
			break;
		case ITEM_MUSIC_RND:
			MediaMusicDrawInfo( MediaMusicFrame.pCanvas, idx );
			MediaMusicDrawItem( MediaMusicFrame.pCanvas, idx, FALSE );
			//dtpresent_flag++;		
			//pthread_cond_signal(&dt_cond);
			//dt_Present();
			dt_Present_part(MediaMusicTouchRect [idx]);
			break;	
		case ITEM_MUSIC_EXIT:
			if (dev_SPIWrite(MediaExit_data,5)!=0)
			printf ("write spi data error!\n");
			MenuMediaInit();
	}
	LOG( "btn = %d\n", idx );	

	return TRUE;
}

void MediaMusicInit()
{
	ir_SetCurrentID( IR_MEDIA_MUSIC_INIT );
	
	frm_Create( &MediaMusicFrame, FRM_NO_TITLE|FRM_NO_TASK, "" );
	MediaMusicDraw( TRUE );
}

/*****************************
function:message_dispatch

 return:
  0: successful
  1: need other menu handle
  -1:failure
*****************************/
static int message_dispatch (void)
{
	int rc = 1;
	

	switch (read_info_data[2])
	{
					case	ID_POWER:						
						break;
						
					case	ID_DISPLAY_SCREEN:
						///////////////for test,because this main board send info error. becarefull!
							switch (read_info_data[3])
								{
									case 	ID_SCREEN_MENU:
											MenuMainInit();
										break;		
									case 	ID_SCREEN_RADIO:
											MenuRadioInit();
										break;									
									case	ID_SCREEN_CDLOAD:											
									case	ID_SCREEN_CDDISP:
									case	ID_SCREEN_MP3DISP:
									case	ID_SCREEN_NODISC:
									case	ID_SCREEN_DISCERR:
									case	ID_SCREEN_EJECT:
											MenuCdInit();
											rece_handle = 0;
										break;
									case	ID_SCREEN_MEDIEA:									
										break;
									
									case 	ID_SCREEN_NAVI:
										//lastID = IR_MEDIA_MUSIC_INIT;	
										MenuNaviInit();
										break;	
										
									case	 	ID_SCREEN_AUDIODISP:
											MenuAudioInit();
											break;	
									case		ID_SCREEN_VIDEODISP:
										MenuVideoInit();
										break;	
									case		ID_SCREEN_EQDISP:
											AudioEQInit();
										break;	
									case	 	ID_SCREEN_BASTRA:
											AudioBASInit();
										break;	
									case		ID_SCREEN_FADBAL:	
											AudioFADInit();							
										break;	
									}

									rc = 0;//if handled, clear this flag.	
								read_info_data[0] = 0;			
																									
						break;
						
					case	ID_KEY_STATUS:
						//it is identify the exit button.	
						
							rc = 0;//if handled, clear this flag.	
							read_info_data[0] = 0;						
						break;
						
					case	ID_SCREEN_INFO:								
								
								if (read_info_data[3] == ID_SCREEN_INFO_VOLUME)
								{
									MenuMusicDrawVolume(MediaMusicFrame.pCanvas,1);	
									break;
								}
								break;
						
					case	ID_KEYPAD:	
							switch (read_info_data[3])
							{
									case 	ID_MUTE:
										MenuMusicDrawVolume(MediaMusicFrame.pCanvas,1);	
										break;
									case	ID_DMUTE:	
										MenuMusicDrawVolume(MediaMusicFrame.pCanvas,3);	
										break;									
									case 	ID_SEEKUP:
									case 	ID_SEEKDN:
////////////////////////////////////////////////// need add 1226 ///////////////////
									break;
									case 	ID_FMAM:
										MenuRadioInit();//band command;
										break;	
									
									case 	ID_DEST:
									case 	ID_NAVI:
										MenuNaviInit();
										break;
									
									case 	ID_CD:
										MenuCdInit();
										break;				
									case 	ID_MEDIA:
										MenuMediaInit();
										break;
									case 	ID_MENU:
										MenuMainInit();
										break;
										
									case	ID_TVAUX:
										
										break;
							}	
							rc = 0;//if handled, clear this flag.	
							read_info_data[0] = 0;						 
						break;
						
					case 	ID_BAND_SEL:
						MenuRadioInit();
						rc = 0;//if handled, clear this flag.	
						read_info_data[0] = 0;			
						break;		
						
					default:	
							rc = -1;						
						}
	return 0;
						
}
void MediaMusicInput(UInt32 key)
{
	int flag;
	
		if ( key == IRC_READ_INFO)
		{
			flag =message_dispatch ();
			if (flag == -1)
				printf ("main menu handle receive data error!\n");
			if (flag == 0)
				{
					//printf ("main menu handle receive data success!\n");
					return;
				}			
		}
	if( key == IRC_TOUCH_DOWN || key == IRC_TOUCH_UP )
	{
		int focus;
		TPosCoor pos;
		dt_ClipTouchGetXY( &pos.x, &pos.y );
		if( key == IRC_TOUCH_DOWN )
		{
			if( LastKey ==IRC_TOUCH_UP )
			{
				if( ( MediaMusicFocus = MediaMusicGetTouchID( pos )) != -1 )
				{
					MediaMusicDrawItem( MediaMusicFrame.pCanvas, MediaMusicFocus, TRUE );
					//dtpresent_flag++;		
					//pthread_cond_signal(&dt_cond);
					//dt_Present();
					dt_Present_part(MediaMusicTouchRect [MediaMusicFocus]);
				}
			}
		}
		else
		{
			if( ( focus = MediaMusicGetTouchID( pos ) ) == MediaMusicFocus )
				{
					if ((MediaMusicFocus != ITEM_MUSIC_EXIT) || ((MediaMusicFocus == ITEM_MUSIC_EXIT)&&(!exit_flag))) //exit valid
					
						{
						MediaMusicBtnEnter( MediaMusicFocus );
						}
				}
			else if( MediaMusicFocus != -1 )
			{
				MediaMusicDrawItem( MediaMusicFrame.pCanvas, MediaMusicFocus, FALSE );
				//dtpresent_flag++;		
				//pthread_cond_signal(&dt_cond);
				//dt_Present();
				dt_Present_part(MediaMusicTouchRect [MediaMusicFocus]);
			}
			MediaMusicFocus = -1;
		}
		LastKey = key;//hangsheng add 071126
	}
	if(mute_status ==2) 
	VolumeCountHandle ();
}
